package com.dengyuanke.admin.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.Goods;
import com.dengyuanke.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-07
 */
@Api(tags = "商品相关API")
@RestController
public class GoodsController {

    final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IGoodsService goodsService;

    /**
     *获取所有商品
     * @param serviceTypeId
     * @param startPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value="获取所有商品", notes="服务类型非必传")
    @PostMapping("/getallgoodsbytype")
    public JsonResult getAllGoodsByType(@ApiParam(value="服务类型Id",name="serviceTypeId",required=false)String serviceTypeId,
                                        @ApiParam(value="开始页",name="startPage",required=true)int startPage,
                                        @ApiParam(value="每页条数",name="pageSize",required=true)int pageSize){
        EntityWrapper<Goods> ew = new EntityWrapper<Goods>();
        if(!StringUtils.isEmpty(serviceTypeId)){
            ew.eq("service_type_id",serviceTypeId);
        }
        ew.eq("is_del",0);
        ew.eq("is_shelving",1);
        Page<Goods> pageGoods = goodsService.selectPage(new Page<Goods>(startPage, pageSize), ew);
        return JsonResult.renderSuccess(pageGoods.getRecords());
    }

    @ApiOperation(value="获取商品详情", notes="必须传入商品id")
    @PostMapping("/getgoodsdetails")
    @ApiParam(value="商品id",name="goodsId",required=true)
    public JsonResult getDetails(String goodsId){
        return JsonResult.renderSuccess(goodsService.selectById(goodsId));
    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @PostMapping("/addgoods")
    public JsonResult addGoods(Goods goods){
        goods.setId(IdWorker.get32UUID());
        goods.setCreateTime(new Date());
        boolean insert = goodsService.insert(goods);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    /**
     * 修改商品
     * @param goods
     * @return
     */
    @PostMapping("/updategoods")
    public JsonResult updateGoods(Goods goods){
        if(StringUtils.isEmpty(goods.getId())){
           return JsonResult.renderFail("传入实体id为空，更新失败");
        }
        boolean b = goodsService.updateById(goods);
        if(b){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }
}
